package com.lewis.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lewis.core.utils.DateUtil;
import com.lewis.core.utils.StringUtil;
import com.lewis.system.entity.SysLogOperational;
import com.lewis.system.mapper.SysLogOperationalMapper;
import com.lewis.system.service.ISysLogOperationalService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 操作日志 服务层处理
 *
 * @author Lewis
 */
@Service
public class SysLogOperationalServiceImpl extends ServiceImpl<SysLogOperationalMapper, SysLogOperational> implements ISysLogOperationalService {

    @Resource
    private SysLogOperationalMapper sysLogOperationalMapper;

    /**
     * 查询系统操作日志集合
     *
     * @param sysLogOperational 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public List<SysLogOperational> list(SysLogOperational sysLogOperational) {
        LambdaQueryWrapper<SysLogOperational> lqw = new LambdaQueryWrapper<SysLogOperational>()
                .like(StringUtil.isNotEmpty(sysLogOperational.getTitle()), SysLogOperational::getTitle, sysLogOperational.getTitle())
                .like(StringUtil.isNotEmpty(sysLogOperational.getOperName()), SysLogOperational::getOperName, sysLogOperational.getOperName())
                .eq(StringUtil.isNotNull(sysLogOperational.getBusinessType()), SysLogOperational::getBusinessType, sysLogOperational.getBusinessType())
                .eq(StringUtil.isNotNull(sysLogOperational.getStatus()), SysLogOperational::getStatus, sysLogOperational.getStatus())
                .orderByDesc(SysLogOperational::getOperTime);

        if (StringUtil.isNotNull(sysLogOperational.getBusinessTypes())) {
            lqw.in(SysLogOperational::getBusinessType, Arrays.asList(sysLogOperational.getBusinessTypes()));
        }

        Map<String, Object> params = sysLogOperational.getParams();
        if (params != null) {
            Object beginTime = sysLogOperational.getParams().get("beginTime");
            if (beginTime != null) {
                lqw.ge(StringUtil.isNotEmpty(String.valueOf(DateUtil.parseDate(sysLogOperational.getParams().get("beginTime")))), SysLogOperational::getOperTime, String.valueOf(DateUtil.parseDate(sysLogOperational.getParams().get("beginTime"))));
            }
            Object endTime = sysLogOperational.getParams().get("endTime");
            if (endTime != null) {
                lqw.le(StringUtil.isNotEmpty(String.valueOf(DateUtil.parseDate(sysLogOperational.getParams().get("endTime")))), SysLogOperational::getOperTime, String.valueOf(DateUtil.parseDate(sysLogOperational.getParams().get("endTime"))));
            }
        }

        return sysLogOperationalMapper.selectList(lqw);
    }
}
